const jsonp = ({url,params,callbackName}) => {
    // 1. 获取URL连接
    const getUrl = () => {
        let dataSrc = ""
        for(let key in params) {
            dataSrc += `${key}=${params[key]}&`
        }
        dataSrc += `callback=${callbackName}`
        return `${url}?${dataSrc}`
    }
    return new Promise((resolve,reject) => {
        // 2. window上挂载回调函数方法
        window[callbackName] = data => {
            resolve(data)
            document.removeChild(scriptEle)
        }
        // 3. 动态生成script标签，并把URL赋值给script的属性
        const scriptEle = document.createElement('script');
        // 4. 执行后端script代码（执行callback函数）
        scriptEle.src = getUrl()
        document.body.appendChild(scriptEle)
    })
}

let api = jsonp({
    url:'http://localhost:3000/img',
    params:{
        name:"qiqi",
        age:"20"
    },
    callbackName:'upload'
})
// 4. 执行
api.then((res) => {
    console.log(res);
})
console.log(api);
